<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span style="font-size: 14px">文章分类</span>
        <el-button type="primary" size="mini" @click="addCate">添加分类</el-button>
      </div>
      <el-table style="width: 100%" border stripe :data="artCateList">
        <el-table-column label="序号" width="100" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="updateCate(scope.row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCate(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog :title="isEdit ? '修改文章分类' : '添加文章分类'" :visible.sync="addVisible" width="35%">
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model.trim="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model.trim="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancel">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmFn">{{ isEdit ? "修改" : "添 加" }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import article from '@/api/article'

export default {
  name: 'ArtCate',
  components: {},
  data () {
    return {
      artCateList: [],
      addVisible: false,
      isEdit: false,
      editId: 0,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须1-15个大小写字母和数字组成的字符串', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  methods: {
    // 1.获取文章列表
    async getArtCateFn () {
      const { data: res } = await article.getCateList()
      this.artCateList = res.data
    },

    // 2.添加文章分类
    addCate () {
      // 表单置空
      this.isEdit = false
      this.editId = 0
      this.addVisible = true
      this.$nextTick(() => {
        this.$refs.addRef.resetFields()
      })
    },

    // 3.取消操作
    cancel () {
      this.addVisible = false
      this.$message.info('已取消操作!')
    },

    // 4.确认添加分类
    confirmFn () {
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          // 新增分类
          if (!this.isEdit) {
            const { data: res } = await article.addCate(this.addForm)
            if (res.code === 0) {
              this.getArtCateFn()
              this.$message.success(res.message)
            } else {
              this.$message.success(res.message)
            }
          } else {
            // 修改分类
            const { data: res } = await article.updateArtDetail({ id: this.editId, ...this.addForm })
            if (res.code === 0) {
              this.getArtCateFn()
              this.$message.success(res.message)
            } else {
              this.$message.success(res.message)
            }
          }
          this.addVisible = false
        } else {
          this.addVisible = false
          return false
        }
      })
    },

    // 5.删除文章分类
    delCate (id) {
      this.$confirm('您确定要永久删除该分类吗?', '提示').then(async () => {
        const { data: res } = await article.deleteArtCate(id)
        if (res.code === 0) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        this.$message.info('已取消删除!')
      }).finally(() => {
        this.getArtCateFn()
      })
    },

    // 6.回显文章分类数据
    async updateCate (id) {
      this.addVisible = true
      this.isEdit = true
      this.editId = id
      const { data: res } = await article.getArtDetail(id)
      if (res.code === 0) {
        this.addForm.cate_name = res.data.cate_name
        this.addForm.cate_alias = res.data.cate_alias
      } else {
        return false
      }
    }
  },
  created () {
    this.getArtCateFn()
  },
  mounted () {

  }
}
</script>
<style scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
